package utils;

import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import org.apache.commons.lang.StringUtils;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author zjh
 */
public class RabbitMqUtil {

    public static Connection init(String host, Integer port, String username, String password, String vHost) {
        //创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        //设置RabbitMQ相关信息
        factory.setHost(host);
        factory.setUsername(username);
        factory.setPassword(password);
        factory.setPort(port);
        if (StringUtils.isNotEmpty(vHost)){
            factory.setVirtualHost(vHost);
        }
        //创建一个新的连接
        try {
            return factory.newConnection();
        } catch (IOException | TimeoutException e) {
            System.out.println(ExceptionUtils.toString(e));
        }
        return null;
    }
}
